package top.milkbox.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import top.milkbox.domain.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Slf4j
@Component
public class HtmlInterceptor extends BaseInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response, Object handler) throws Exception {

        if (isUserLogin(request.getCookies())) {
            log.debug("用户已登录，允许访问网页：" + request.getRequestURL());
            return true; // 通过
        } else {
            log.debug("session中未查询到用户信息，拦截网页：" + request.getRequestURL());
            log.debug("正在重定向到登录页面...");
            // 重定向到登录页面
            response.sendRedirect("/login.html");
            return false; // 拦截
        }
    }

}
